import java.util.Deque;
import java.util.LinkedList;

public class Calculate {

    public static void main(String[] args) {
        Calculate calculate = new Calculate();
        System.out.println(calculate.calculate(" 3+5 / 2 "));
    }

    Deque<Integer> stackInteger;
    Deque<Character> stackOp;

    public int calculate1(String s) {
        char[] chars = s.toCharArray();
        stackInteger = new LinkedList<>();
        stackOp = new LinkedList<>();
        for (int i = 0; i < s.length(); i++) {
            if (chars[i] == ' ') continue;
            if (Character.isDigit(chars[i])) {
                boolean flag = i != 0 && chars[i - 1] == '-';
                StringBuilder stringBuilder = new StringBuilder();
                while (i < s.length() && Character.isDigit(chars[i])) {
                    stringBuilder.append(chars[i++]);
                }
                --i;
                int i3 = Integer.parseInt(stringBuilder.toString());
                stackInteger.push(flag ? -i3 : i3);
                if (flag) stackOp.push('+');
                if (!stackOp.isEmpty() && (stackOp.peek() == '*' || stackOp.peek() == '/')) {
                    cal();
                }
            } else if (chars[i] != ')' && chars[i] != '-') {
                stackOp.push(chars[i]);
            } else if (chars[i] == ')') {
                while (stackOp.peek() != '(') {
                    cal();
                }
                stackOp.poll();
                if (!stackOp.isEmpty() && (stackOp.peek() == '*' || stackOp.peek() == '/')) {
                    cal();
                }
            }
        }
        while (!stackOp.isEmpty()) {
            cal();
        }
        return stackInteger.poll();
    }

    public int process(int i, int j, char c) {
        if (c == '*') {
            return i * j;
        } else {
            return i / j;
        }
    }

    public void cal() {
        Integer i1 = stackInteger.poll();
        Integer i2 = stackInteger.poll();
        stackInteger.push(process(i2, i1, stackOp.poll()));
    }

    public int calculate(String s) {
        s = s.replaceAll(" ", "");
        char[] chars = s.toCharArray();
        int n = chars.length;
        Deque<Integer> stackInteger = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            int temp = chars[i];
            if (Character.isDigit(temp)) {
                char c = i != 0 ? chars[i - 1] : '#';
                int num = 0;
                while (i < n && Character.isDigit(chars[i])) {
                    num = num * 10 + chars[i] - '0';
                    ++i;
                }
                --i;
                if (c == '*' || c == '/') {
                    stackInteger.push(process(stackInteger.poll(), num, c));
                } else {
                    stackInteger.push(c == '-' ? -num : num);
                }
            }
        }
        int ans = 0;
        while (!stackInteger.isEmpty()) {
            ans += stackInteger.poll();
        }
        return ans;
    }
}
